package com.taichuan.selfcheck.ad;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
 * Created by OISNY on 2016/7/14.
 */
public class VideoSurfaceView extends SurfaceView implements
        SurfaceHolder.Callback, ADViewInterface.VideoPresenterInterface {
    private SurfaceHolder mSurfaceHolder;
    private ADVideoPresenter mVideoPresenter;

    public VideoSurfaceView(Context context, ADVideoPresenter presenter) {
        super(context);
//        setBackgroundColor(Color.BLACK);
        setZOrderOnTop(true);
        setZOrderMediaOverlay(true);
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        getHolder().addCallback(this);
        mVideoPresenter = presenter;
        mVideoPresenter.setVideoPresenterInterface(this);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        Log.d(getClass().getSimpleName(), "MediaPlayer VideoSurfaceView surfaceCreated !");
        mSurfaceHolder = surfaceHolder;
        mVideoPresenter.play(surfaceHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        mSurfaceHolder = surfaceHolder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.d(getClass().getSimpleName(), "MediaPlayer VideoSurfaceView surfaceDestroyed !");
        mVideoPresenter.release();
    }

    @Override
    public void onSurfaceViewChanged(int width, int height) {
/*        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(1024,
                (int) (height * (float) 1024 / (float) width));
        lp.gravity = Gravity.CENTER;
        // 设置显示大小
        setLayoutParams(lp);*/
    }
}
